package top.yokey.android;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.google.android.material.snackbar.Snackbar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class RegisterActivity extends AppCompatActivity {

    private AppCompatEditText mobileEditText;
    private AppCompatEditText passwordEditText;
    private AppCompatEditText nicknameEditText;
    private AppCompatTextView registerTextView;
    private View mobileView, passwordView, nicknameView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置默认的布局文件
        setContentView(R.layout.activity_register);

        mobileEditText = findViewById(R.id.mobileEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        nicknameEditText = findViewById(R.id.nicknameEditText);
        registerTextView = findViewById(R.id.registerTextView);
        mobileView = findViewById(R.id.mobileView);
        passwordView = findViewById(R.id.passwordView);
        nicknameView = findViewById(R.id.nicknameView);

        mobileEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.primary);
                passwordView.setBackgroundResource(R.color.textHint);
                nicknameView.setBackgroundResource(R.color.textHint);
            }
        });

        passwordEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.primary);
                nicknameView.setBackgroundResource(R.color.textHint);
            }
        });

        nicknameEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.textHint);
                nicknameView.setBackgroundResource(R.color.primary);
            }
        });

        registerTextView.setOnClickListener(view -> {
            String mobile = mobileEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            String nickname = nicknameEditText.getText().toString();
            if (mobile.isEmpty()) {
                //吐司提醒 Snackbar;
                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                Snackbar.make(registerTextView, "请输入手机号", Snackbar.LENGTH_SHORT).show();
                return;
            }
            if (password.isEmpty()) {
                //吐司提醒 Snackbar;
                Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (nickname.isEmpty()) {
                //吐司提醒 Snackbar;
                Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
                return;
            }
            //发送注册请求,注意IP地址改成自己本机的IP地址
            String url = "http://192.168.8.61/api/base/register?mobile=" + mobile + "&password=" + password + "&nickname=" + nickname;
            OkGo.<String>get(url).execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    System.out.println(response.body());
                }

                @Override
                public void onError(Response<String> response) {
                    super.onError(response);
                    System.out.println(response.getException().toString());
                }
            });
        });

    }

}
